/*     */ package com.cr.common.utils.file;
/*     */ 
/*     */ import com.cr.common.config.RuoYiConfig;
/*     */ import com.cr.common.exception.file.FileNameLengthLimitExceededException;
/*     */ import com.cr.common.exception.file.FileSizeLimitExceededException;
/*     */ import com.cr.common.exception.file.InvalidExtensionException;
/*     */ import com.cr.common.utils.MyDateUtils;
/*     */ import com.cr.common.utils.MyStringUtils;
/*     */ import com.cr.common.utils.uuid.IdUtils;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import org.apache.commons.io.FilenameUtils;
/*     */ import org.springframework.web.multipart.MultipartFile;
/*     */ 
/*     */ public class FileUploadUtils {
/*     */   public static final long DEFAULT_MAX_SIZE = 52428800L;
/*     */   
/*     */   public static final int DEFAULT_FILE_NAME_LENGTH = 100;
/*     */   
/*  36 */   private static String defaultBaseDir = RuoYiConfig.getProfile();
/*     */   
/*     */   public static void setDefaultBaseDir(String defaultBaseDir) {
/*  40 */     FileUploadUtils.defaultBaseDir = defaultBaseDir;
/*     */   }
/*     */   
/*     */   public static String getDefaultBaseDir() {
/*  45 */     return defaultBaseDir;
/*     */   }
/*     */   
/*     */   public static final String upload(MultipartFile file) throws IOException {
/*     */     try {
/*  59 */       return upload(getDefaultBaseDir(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
/*  61 */     } catch (Exception e) {
/*  63 */       throw new IOException(e.getMessage(), e);
/*     */     } 
/*     */   }
/*     */   
/*     */   public static final String upload(String baseDir, MultipartFile file) throws IOException {
/*     */     try {
/*  79 */       return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
/*  81 */     } catch (Exception e) {
/*  83 */       throw new IOException(e.getMessage(), e);
/*     */     } 
/*     */   }
/*     */   
/*     */   public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension) throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException, InvalidExtensionException {
/* 103 */     int fileNamelength = file.getOriginalFilename().length();
/* 104 */     if (fileNamelength > 100)
/* 106 */       throw new FileNameLengthLimitExceededException(100); 
/* 109 */     assertAllowed(file, allowedExtension);
/* 111 */     String fileName = extractFilename(file);
/* 113 */     File desc = getAbsoluteFile(baseDir, fileName);
/* 114 */     file.transferTo(desc);
/* 115 */     String pathFileName = getPathFileName(baseDir, fileName);
/* 116 */     return pathFileName;
/*     */   }
/*     */   
/*     */   public static final String extractFilename(MultipartFile file) {
/* 124 */     String fileName = file.getOriginalFilename();
/* 125 */     String extension = getExtension(file);
/* 126 */     fileName = MyDateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + extension;
/* 127 */     return fileName;
/*     */   }
/*     */   
/*     */   public static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException {
/* 132 */     File desc = new File(uploadDir + File.separator + fileName);
/* 134 */     if (!desc.exists())
/* 136 */       if (!desc.getParentFile().exists())
/* 138 */         desc.getParentFile().mkdirs();  
/* 141 */     return desc;
/*     */   }
/*     */   
/*     */   public static final String getPathFileName(String uploadDir, String fileName) throws IOException {
/* 146 */     int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
/* 147 */     String currentDir = MyStringUtils.substring(uploadDir, dirLastIndex);
/* 148 */     String pathFileName = "/profile/" + currentDir + "/" + fileName;
/* 149 */     return pathFileName;
/*     */   }
/*     */   
/*     */   public static final void assertAllowed(MultipartFile file, String[] allowedExtension) throws FileSizeLimitExceededException, InvalidExtensionException {
/* 163 */     long size = file.getSize();
/* 164 */     if (size > 52428800L)
/* 166 */       throw new FileSizeLimitExceededException(50L); 
/* 169 */     String fileName = file.getOriginalFilename();
/* 170 */     String extension = getExtension(file);
/* 171 */     if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension)) {
/* 173 */       if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION)
/* 175 */         throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension, fileName); 
/* 178 */       if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION)
/* 180 */         throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension, fileName); 
/* 183 */       if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION)
/* 185 */         throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension, fileName); 
/* 188 */       if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION)
/* 190 */         throw new InvalidExtensionException.InvalidVideoExtensionException(allowedExtension, extension, fileName); 
/* 195 */       throw new InvalidExtensionException(allowedExtension, extension, fileName);
/*     */     } 
/*     */   }
/*     */   
/*     */   public static final boolean isAllowedExtension(String extension, String[] allowedExtension) {
/* 209 */     for (String str : allowedExtension) {
/* 211 */       if (str.equalsIgnoreCase(extension))
/* 213 */         return true; 
/*     */     } 
/* 216 */     return false;
/*     */   }
/*     */   
/*     */   public static final String getExtension(MultipartFile file) {
/* 227 */     String extension = FilenameUtils.getExtension(file.getOriginalFilename());
/* 228 */     if (MyStringUtils.isEmpty(extension))
/* 230 */       extension = MimeTypeUtils.getExtension(file.getContentType()); 
/* 232 */     return extension;
/*     */   }
/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-common-4.7.0.jar!\com\cr\commo\\utils\file\FileUploadUtils.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */